package com.lyrcsoft.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : fujc-dev@qq.com
 * @created : 2022/10/12 14:20
 */
@ResponseBody
@Controller
@RequestMapping("/user")
public class UserController {


    @Autowired
    private RestTemplate restTemplate;

    // 为什么加一个直接就能帮助我们实现熔断、降级的处理？
    // 动态代理
    //@HystrixCommand(fallbackMethod = "queryUserInfoByIdFallback")
    @GetMapping("/queryUserInfoById")
    public Map<String, Object> queryUserInfoById() {
        System.out.println("1");
        String uri = "http://MS-PROVIDER/order/getDefaultUserInfo";
        return restTemplate.getForObject(uri, Map.class);
    }

    //熔断，多次降级，不会调用该方法，直接返回本地方法

    /**
     * 降级（出错了，返回本地的降级方法）
     *
     * <p>
     * fallbackMethod 与原方法参数与返回对象必须一致
     * </p>
     */
    public Map<String, Object> queryUserInfoByIdFallback() {
        Map<String, Object> containers = new HashMap<>();
        containers.put("id", 10001);
        containers.put("username", "admin");
        containers.put("target", "admin");
        return containers;
    }
}
